package service.impl;

import dao.PlanDao;
import dao.impl.*;
import pojo.*;
import service.UsecaseService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsecaseServiceImpl implements UsecaseService {
    private PlanDao planDao = new PlanDaoImpl();

    @Override
    public <T> int saveUsecase(Class<T> clazz, Plan plan, T usecase) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, SQLException {
        int count = 0;
        if(planDao.saveplan(plan) == 1) count++;

        Object dao = clazz.getConstructor().newInstance();
        Method[] methods = clazz.getMethods();
        for (Method method : methods){
            if (method.getName().matches("save")){
                if((int)method.invoke(dao,usecase) == 1) count++;
            }
        }
        return count;
    }

    @Override
    public List<Test> getAllUsecase(String uid) throws SQLException {
        List<Test> tests = new ArrayList<>();

        List<Plan> plans = planDao.getAllPlan(uid);
        plans.forEach( plan -> {
            String module = plan.getModule();
            System.out.println("getAllUsecase-> module=="+module);
            try {
                if (module.equals("collect")){
                    Test<Collect> test = new Test<>(plan.getTime(),plan.getCid(),plan.getModule(),new CollectDaoImpl().getOneCollect(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("comment")) {
                    Test<Comment> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new CommentDaoImpl().getOneComment(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("like")){
                    Test<Like> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new LikeDaoImpl().getOneLike(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("login")){
                    Test<Login> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new LoginDaoImpl().getOneLogin(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("loginicon")){
                    Test<LoginIcon> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new LoginIconDaoImpl().getOneLoginIcon(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("page")){
                    Test<Page> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new PageDaoImpl().getOnePage(plan.getCid()));
                    tests.add(test);
                }else if(module.equals("sort")){
                    Test<Sort> test = new Test<>(plan.getTime(), plan.getCid(), plan.getModule(), new SortDaoImpl().getOneSort(plan.getCid()));
                    tests.add(test);
                }else {
                    System.out.println("getAllUsecase-> 未找到module");
                }

            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException(e);
            }

        });
        return tests;
    }
}
